package cn.livingCloud.service.impl;

import cn.livingCloud.dto.SettingDto;
import cn.livingCloud.dto.SettingFromDto;
import cn.livingCloud.dto.SystemProfileDto;
import cn.livingCloud.entity.system.Setting;
import cn.livingCloud.repository.SettingMapper;
import cn.livingCloud.service.SettingService;
import cn.livingCloud.service.impl.operation.SettingFromDtoUpdator;
import org.hyperic.sigar.Sigar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by DingJie  on 2016/1/19.
 */
@Service
public class SettingServiceImpl implements SettingService {
    @Autowired
    private SettingMapper mapper;

    @Override
    public SettingFromDto loadFromDto() {
        Setting setting = mapper.findSetting();
        if (setting == null) {
            return new SettingFromDto();
        }
        return new SettingFromDto(setting);
    }

    @Override
    public void update(SettingFromDto settingFromDto) {
        SettingFromDtoUpdator updator = new SettingFromDtoUpdator(settingFromDto);
        updator.update();

    }

    @Override
    public SettingDto findSetting() {
        return new SettingDto(mapper.findSetting());
    }

    @Override
    public String getAppid() {
        return mapper.findSetting().getAppid();
    }

    @Override
    public SystemProfileDto getProfile() {
        Sigar sigar = new Sigar();
        //        double cpuUsedPerc = sigar.getCpuPerc().getCombined();//cpu
//        double memUsed = sigar.getMem().getActualUsed();//mem
//        double memTotal = sigar.getMem().getTotal();
//        double memUsedPerc = sigar.getMem().getUsedPercent();
//        String memUsedStr = String.format("%.2f", memUsed / 1024 / 1024 / 1024) + "GB";// mem to string GB
//        String memTotalStr = String.format("%.2f", memTotal / 1024 / 1024 / 1024) + "GB";
//        String memUsedPercStr = String.format("%.2f", memUsedPerc) + " %";
//        double diskUsed = sigar.getFileSystemUsage("E:\\").getUsed();//disk
//        double diskTotal = sigar.getFileSystemUsage("E:\\").getTotal();
//        double diskUsedPerc = sigar.getFileSystemUsage("E:\\").getUsePercent();
//        String diskUsedStr = String.format("%.2f", diskUsed / 1024 / 1024) + "GB";//disk to String GB
//        String diskTotalStr = String.format("%.2f", diskTotal / 1024 / 1024) + "GB";
//        String diskUsedPercStr = String.format("%.2f", diskUsedPerc * 100) + " %";
//        String fqdn = sigar.getFQDN();//IP地址
        return new SystemProfileDto(sigar);
    }
}
